package cn.com.api.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.api.entity.ApiT_member;
import cn.com.api.entity.ApiU_advert;
import cn.com.api.entity.ApiU_bonus;
import cn.com.api.entity.ApiU_bonus_detail;
import cn.com.api.entity.ApiU_message;
import cn.com.api.mapper.ApiU_bonusMapper;
import cn.com.api.mapper.ApiU_bonus_detailMapper;
import cn.com.api.mapper.ApiU_messageMapper;
import cn.com.api.mapper.ApiT_memberMapper;
import cn.com.api.mapper.ApiU_advertMapper;

import cn.com.api.service.ApiU_bonusService;
import cn.com.api.service.impl.base.U_bonusServiceImplBase;
import cn.com.util.DateUtil;
import cn.com.util.PageInfo;
import cn.com.util.Util;

/**
 * 
 **/
@Service
public class ApiU_bonusServiceImpl extends U_bonusServiceImplBase implements ApiU_bonusService {
	@Autowired
	private ApiU_bonusMapper apiU_bonusMapper;
	@Autowired
	private ApiU_advertMapper advertMapper;
	@Autowired
	private ApiT_memberMapper apiT_memberMapper;
	@Autowired
	private ApiU_bonus_detailMapper apiU_bonus_detailMapper;
	@Autowired
	private ApiU_messageMapper apiU_messageMapper;

	@Override
	public ApiU_bonus findBonusByID(Integer bonus_id) {
		// 需要考虑赠送的红包,红包赠送就相当于领取了
		return apiU_bonusMapper.findU_bonusByID(bonus_id);
	}

	@Override
	public ApiU_bonus findNormalBonusHis(String bonus_id) {
		// // 检查是否领取过红包
		// ApiU_bonus_detail hasDetail =
		// this.apiU_bonus_detailMapper.checkAvailableBonus(bonus_id, user_id);
		// if (Util.isNull(hasDetail))
		// {
		// //没有领过
		//
		// }
		ApiU_bonus bonus = apiU_bonusMapper.findU_bonusById(Integer.valueOf(bonus_id));
		if (!Util.isNull(bonus)) {
			List<ApiU_bonus_detail> bonus_detail_list = apiU_bonus_detailMapper.getNormalBonusList(bonus_id);
			bonus.setBonus_details(bonus_detail_list);
		}
		return bonus;
	}

	// 新增对象后返回记录ID
	public int U_bonusInsertion(ApiU_bonus u_bonus) {
		return this.apiU_bonusMapper.U_bonusInsertion(u_bonus);
	}

	public List<ApiU_bonus> findBonusByMap(Map map) {
		return this.apiU_bonusMapper.findBonusByMap(map);
	}

	public ApiU_bonus getBonusInfoByMap(Map map) {
		return this.apiU_bonusMapper.getBonusInfoByMap(map);
	}

	public void deleteBonus(Integer bonusid) {
		apiU_bonusMapper.deleteBonus(bonusid);
		// 查看是否有广告
		ApiU_advert advert = advertMapper.findBonusAdvertByBonusID(bonusid + "");
		if (advert != null) {
			advertMapper.deleteAdvert(advert.getId());
		}
	}

	// 根据广告ID获取红包明细
	public ApiU_bonus getBonusInfoByAdvertID(String advert_id) {
		return this.apiU_bonusMapper.findBlesswordsByAdvertID(advert_id);
	}

	// 收回红包金额
	@Transactional
	public void takeBackBonus(Integer hours) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("hours", hours);
		// 更新检查过期的拼手气红包，过期则将金额返还到发红包的人
		List<ApiU_bonus> list = apiU_bonusMapper.getExprieBonus(hours);
		List<ApiU_message> messagelist = new ArrayList<ApiU_message>();
		for (ApiU_bonus bonus : list) {
			ApiT_member apiT_member = apiT_memberMapper.findT_memberById(Integer.valueOf(bonus.getUser_id()));
			BigDecimal balance = bonus.getSummy().subtract(bonus.getReceived_amount());
			// 将红包的剩余的钱加入用户余额中
			apiT_memberMapper.updateMemberMoneyByUserId(bonus.getUser_id(), apiT_member.getC_money().add(balance));

			// 加入消息中
			ApiU_message apiU_message = new ApiU_message();
			apiU_message.setAdd_time(DateUtil.nowTimeToString());
			apiU_message.setContent("红包已过期，剩余金额：" + balance + "元未领取，现已退回到你的账户余额中。");
			apiU_message.setUser_id(bonus.getUser_id());
			apiU_message.setType(4);
			apiU_message.setTitle("红包退款");
			apiU_message.setConn_id(bonus.getId());
			apiU_message.setIs_read(0);
			messagelist.add(apiU_message);

			// 更新红包的过期状态
			bonus.setIs_expire(1);
			apiU_bonusMapper.updateU_bonus(bonus);
		}

		//// 更新过期的转发、分享的红包；过期则将金额返还到分享的人或者转发的人账户中
		List<ApiU_bonus_detail> details = apiU_bonus_detailMapper.getUnclaimedBonusDetail(hours);
		for (ApiU_bonus_detail apiU_bonus_detail : details) {
			ApiT_member apiT_member = apiT_memberMapper
					.findT_memberById(Integer.valueOf(apiU_bonus_detail.getGet_uid()));
			// 将红包的剩余的钱加入用户余额中
			apiT_memberMapper.updateMemberMoneyByUserId(apiU_bonus_detail.getGet_uid(),
					apiT_member.getC_money().add(apiU_bonus_detail.getMoney()));

			// 加入消息中
			ApiU_message apiU_message = new ApiU_message();
			apiU_message.setAdd_time(DateUtil.nowTimeToString());
			apiU_message.setContent("您分享或转发的红包未被领取，红包金额：" + apiU_bonus_detail.getMoney() + "元现已退回到你的账户余额中。");
			apiU_message.setUser_id(apiU_bonus_detail.getGet_uid());
			apiU_message.setType(4);
			apiU_message.setTitle("红包退款");
			apiU_message.setConn_id(apiU_bonus_detail.getId());
			apiU_message.setIs_read(0);
			messagelist.add(apiU_message);

			// 更新红包的过期状态
			apiU_bonus_detail.setStatus(1);
			apiU_bonus_detail.setIs_forward(0);
			apiU_bonus_detailMapper.updateU_bonus_detail(apiU_bonus_detail);
		}
		apiU_messageMapper.insertmessageList(messagelist);

	}

	// 发出的红包列表
	public List<ApiU_bonus> getSendNormalBonusList(String user_id, PageInfo page) {
		return this.apiU_bonusMapper.getSendNormalBonusListPage(user_id, page);
	}
}
